<template>
	<view class="pad-10">
		<!-- 开单记录 -->
		<view v-for="(item,index) in dataSource" :key="index" class="item">
			<view class="start-center">
				<view class="font-weight flex-1">{{item.name}}</view>
				<view>{{item.medicalTypeName}}</view>
			</view>
			<view class="color-gray">
				<view class="start-center">
					<view class="base-title">就诊人</view>
					<view>：{{item.patName}}</view>
				</view>
				<view class="start-center">
					<view class="base-title">医生</view>
					<view>：{{item.doctorName}} {{item.departmentName}}</view>
				</view>
				<view class="start-center">
					<view class="base-title">科室</view>
					<view>：{{item.departmentName}}</view>
				</view>
				<view class="start-center" v-if="item.money">
					<view class="base-title">费用</view>
					<view>：<text class="color-red">￥{{item.money}}</text></view>
				</view>
				<view class="start-center">
					<view class="base-title">开单时间</view>
					<view>：{{item.createTime}}</view>
				</view>
				<view class="start-center">
					<view class="base-title">处方编号</view>
					<view>：{{item.preId}}</view>
				</view>
				<view class="start-center" v-if="item.visitTime">
					<view class="base-title">预约日期</view>
					<view>：{{item.visitTime}}</view>
				</view>
				<view class="start-center">
					<view class="base-title">开单状态</view>
					<view v-if="item.payStatus==0">：未支付</view>
					<view v-if="item.payStatus==1">：已支付</view>
					<view v-if="item.payStatus==2">：已退款</view>
					<view v-if="item.payStatus==3">：已取消</view>
					<view v-if="item.payStatus==4">：已预约</view>
				</view>
				<view class="start-center" v-if="item.describe">
					<view class="base-title">描述</view>
					<view>：{{item.describe}}</view>
				</view>
				<view v-if="item.payStatus==0">
					<view v-if="item.canReserve==1">
						<!-- 如果已经预约成功，则有申请单号,预约编号会在签到时更新 -->
						<view v-if="item.applyBillNo">
							<ButtonAllVue @cancel="openCancelDialog(item)" btn-l="取消开单" btn-r="去支付"
								@click="checkSelfBilliingOrder(item)"></ButtonAllVue>
						</view>
						<view v-else>
							<ButtonAllVue v-if="item.visitTime" @cancel="openCancelDialog(item)" btn-l="取消开单"
								btn-r="预约详情" @click="toReserveDetailPage(item)"></ButtonAllVue>
							<ButtonAllVue v-else @cancel="openCancelDialog(item)" btn-l="取消开单" btn-r="去预约"
								@click="toBookPage(item)"></ButtonAllVue>
						</view>
					</view>
					<ButtonAllVue v-if="item.canReserve==0" @cancel="openCancelDialog(item)" btn-l="取消开单" btn-r="去支付"
						@click="checkSelfBilliingOrder(item)"></ButtonAllVue>
				</view>
				<view v-if="item.payStatus==1">
					<ButtonAllVue v-if="item.canReserve==1" @cancel="openRefundDialog(item)" btn-l="开单退费" btn-r="预约详情"
						@click="toReserveDetailPage(item)"></ButtonAllVue>
					<ButtonAllVue v-if="item.canReserve==0" @cancel="openRefundDialog(item)" btn-l="开单退费">
					</ButtonAllVue>
				</view>
			</view>
		</view>
		<Dialog :show="showCancel" @close="showCancel=false" @confirm="confirmCancel">
			<view class="slot-content">
				<view class="text-center">
					<rich-text nodes="确定取消开单吗?"></rich-text>
				</view>
			</view>
		</Dialog>
		<Dialog :show="showRefund" @close="showRefund=false" @confirm="confirmRefund">
			<view class="slot-content">
				<view class="text-center">
					<rich-text nodes="确定要退费吗?"></rich-text>
				</view>
			</view>
		</Dialog>
		<Dialog :show="showPay" @close="showPay=false" @confirm="confirmPay">
			<view class="slot-content">
				<view>
					<view class="start-center">
						<view>项目名称：</view>
						<view>{{dataRecord.name}}</view>
					</view>
					<view class="start-center">
						<view>开单金额：</view>
						<view class="color-red">￥{{dataRecord.money}}</view>
					</view>
					<view class="start-center">
						<view>开单时间：</view>
						<view>{{dataRecord.createTime}}</view>
					</view>
					<view class="color-red font-size-20">还有未支付开单，确定要支付吗?</view>
				</view>
			</view>
		</Dialog>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		selfBillingSelect,
		selfBillingCancel,
		selfBillingRefund
	} from '../../../../../api/selfBilling'
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import {
		dateFormat,
		getDateBySecond
	} from '../../../../../utils/assist'
	import ButtonAllVue from '../../../../../components/Button/ButtonAll.vue'
	import DialogVue from '../../../../../components/Dialog/Dialog.vue'
	import {
		selectPayOrder
	} from '../../../../../api/payOrder'
	export default {
		props: {
			dataSource: Array,
		},
		components: {
			ButtonAllVue,
			DialogVue
		},
		data() {
			return {
				// dataSource: []
				showCancel: false,
				showRefund: false,
				showPay: false,
				dataRecord: '', //开单记录点击时选择的数据,取消，退费，去支付，去预约
			}
		},
		computed: {
			...mapGetters([
				'userInfo', 'currentHospitalInfo'
			]),
		},
		mounted() {},
		methods: {
			...mapActions(['createPay']),
			openCancelDialog(item) {
				this.showCancel = true;
				this.dataRecord = item;
			},
			openRefundDialog(item) {
				this.showRefund = true;
				this.dataRecord = item;
			},
			confirmCancel() {
				this.dataRecord.payStatus = 3
				this.dataRecord.payStatusName = "已取消"
				selfBillingCancel(this.dataRecord).then(res => {
					this.$refs.uToast.show({
						title: '取消成功',
						type: 'success',
					})
					this.showCancel = false;
					this.$emit("onChangeTab", "history")
				})

			},
			confirmRefund() {
				this.dataRecord.payStatus = 2
				this.dataRecord.payStatusName = "已退款"
				// this.dataRecord.orderId = "20250919100839_14"
				// this.dataRecord.money = "2"
				selfBillingRefund(this.dataRecord).then(res => {
					this.$refs.uToast.show({
						title: '退款成功',
						type: 'success',
					})
					this.showRefund = false;
					this.$emit("onChangeTab", "history")
				})

			},
			checkSelfBilliingOrder(item) {
				this.dataRecord = item;
				// 去支付前，先查询是否有未支付订单，如果有，则先支付，
				// 没有则新增订单到订单表
				let orderTime = getDateBySecond(-900);
				selectPayOrder({
					patId: this.userInfo.patId,
					orderTime: orderTime,
					paySourceType: 5, //自助开单
					payStatus: 1,
					page: 1,
					size: 5,
					orderBy: "create_time desc"
				}).then(res => {
					console.log(res)
					if (res.length > 0) {
						this.showPay = true;
					} else {
						this.createOrder()
					}

				})
			},
			confirmPay() {
				this.toPayPage()
			},
			createOrder() {
				this.form = {
					openId: this.userInfo.openId,
					patId: this.userInfo.patId,
					patName: this.userInfo.patName,
					payMoney: this.dataRecord.money,
					campusCode: this.currentHospitalInfo.code,
					campusName: this.currentHospitalInfo.name,
					departmentCode: this.dataRecord.departmentCode,
					departmentName: this.dataRecord.departmentName,
					departmentAddress: this.dataRecord.departmentAddress,
					doctorCode: this.dataRecord.doctorCode,
					doctorName: this.dataRecord.doctorName,
					doctorPhone: this.dataRecord.doctorPhone,
					doctorHead: this.dataRecord.doctorHead,
					status: 1,
					jobTitle: this.dataRecord.jobTitle,
					paySourceType: 5,
					paySourceName: "自助开单缴费", // 1门诊缴费 2急诊缴费 3问诊缴费 4挂号缴费 5自助开单缴费
					regType: "0", // 0:普通挂号 1:急诊挂号 2:专家挂号 3:点名专家 4:特殊挂号 5:义诊 6:外宾挂号 9 预约挂号
					regCampus: this.currentHospitalInfo.name,
					operatorDeptCode: this.dataRecord.execDeptCode,
					discountMoney: 0, //优惠金额
					payStatus: 1,
					payStatusName: '待支付，已下单',
					deductAccountType: 1,
					regId: this.dataRecord.preId //自助开单处方id，放入挂号id里面
				}
				this.createPay(this.form).then(res => {
					this.toPayPage()
				})
			},
			toPayPage() {
				// 去支付
				this.dataRecord.paySourceType = 5;
				uni.navigateTo({
					url: '/pages/Pay/SelfBillingPay?item=' + JSON.stringify(this.dataRecord)
				})
			},
			toBookPage(item) {
				// 去预约
				uni.navigateTo({
					url: '/pages/Home/ServePatient/SelfBillingBook?item=' + JSON.stringify(item)
				})
			},
			toReserveDetailPage(item) {
				uni.navigateTo({
					url: '/pages/Home/ServePatient/SelfBillingBookRecord?item=' + JSON.stringify(item)
				})
			}
		}
	}
</script>

<style scoped>
	.base-title {
		width: 80px;
		text-align: justify;
		line-height: 30px;
		text-align-last: justify;
	}

	.item {
		margin: 10px 0;
		background: #fafafa;
		padding: 10px;
		border-radius: 10px;
	}
</style>